<?php
// this page includes the server side of application, exluding the admin part, sent to the client and displayed
// Also the send mail options
session_start();
if (!isset($_POST['do']))
	{
		exit;
	}
?>
<script src="inc/js/core.js" type="text/javascript"></script>
<script src="inc/js/jquery.timeago.js" type="text/javascript"></script>

<?php	
require ('./inc/database.php');
	require ('./inc/functions.php');
	require ('./config.php');
	require_once('./class/class.phpmailer.php');
	require_once('./class/class.user.php');
	require_once('./class/class.project.php');
	require_once('./class/class.task.php');
	require_once('./class/class.project_access_details.php');
	require_once('./class/class.permissions.php');
	require_once('./class/class.comments.php');
	require_once('./class/class.dashboard.php');
	db_connect();	
if($_POST['do']=='show_project_ajax')//press project name will show all tasks and information of the project
		{
		  $id=$_POST['id']; 
		  $session=$_POST['name_session'];
		  $proj=new project();
		  $bool=$proj->getby_id($id);
		  $_SESSION['project']=$proj->getid();
		  $user=new user();
		  $user->getby_name($session);
		  ?>
           <span id="name_project" class="color_w"><?php echo  $proj->project_name ?></span>
            <?php $proj->get_html_project(); ?><!-- project information -->
           <input id="id_project" name="id_project" type="hidden" value="<?php echo $proj->getid();?>"/>
           <?php $proj->selector(); ?>
           <div id="all_task">
		   <?php $task=new task();
		         $arr=$proj->get_tasks(100);//all tasks array
				 if($arr!=0)
					  {
					 foreach($arr as $task)
						{
							{
								$task=task::createTaskFromArray($task);
								$task->get_html_task();
							}
						}
					  }
						 
  			?>
          </div>
          <script>
            
                var x=<?php echo $user->role_type; ?>;
				$('#span_send_task').css('display','block');
				/*if(x==2 || x==3)
					{
					  $('#span_send_task').css('display','none');	
					}
				else 
				   {	
					$('#span_send_task').css('display','block');	
				   }*/
		
          </script>
		  <?php 
		  
			
		}//end show_project_ajax
if($_POST['do']=='show_project_ajax_find')// when clicking on a project all project info page is shown
		{
			
		  $name_project=htmlspecialchars_decode($_POST['name_project'],ENT_COMPAT);
		  $session=$_POST['name_session'];
		  $proj=new project();
		  
		 $bool=$proj->getby_name($name_project);

		 $_SESSION['project']=$proj->getid();
		 $user=new user();
		 $user->getby_name($session);
		 
		 
		 if( !$bool || ($proj->project_status==1 &&  !($user->role_type==1 || $user->role_type==6)) || !(permissions::is_permissions($user->getuser_id(),$proj->getid()) )&& !($user->role_type==1)){?>
			 <label> אין פרוייקט כזה במערכת</label> <?php  exit;}
		 
		 
		 	 
		  ?>
          <span id="name_project" class="color_w"><?php echo  $proj->project_name ?></span>
            <?php $proj->get_html_project(); ?><!--project details-->
        
          <input id="id_project" name="id_project" type="hidden" value="<?php echo $proj->getid();?>"/>
          <?php $proj->selector(); ?>
            <div id="all_task">
            <?php  $task=new task();
		         $arr=$proj->get_tasks();//task array of this project
				  if($arr!=0)
				 {
				 foreach($arr as $task)
				 	{
						  {
							 $task=task::createTaskFromArray($task);
							  $task->get_html_task();//shows all comments and tasks
						  }
					}
				 }
			?>
            
          </div>
           <script>
            $(document).ready(function(e) {
				$('#hid_id_project').val($('#id_project').val());
                var x=<?php echo $user->role_type; ?>;
				if(x==2 || x==3)
					{
					  $('#span_send_task').css('display','none');	
					}
				else 
				   {	
					$('#span_send_task').css('display','block');	
				   }
			});
          </script>
		  <?php 
		  
		}//end show_project_ajax_find
if($_POST['do']=='selector_all_option')//all task of a project by status and time
{
 $id_project=$_POST['id_project'];
 $status=$_POST['status'];
 $from=$_POST['from'];
 $to=$_POST['to'];	
 $project=new project();
 $project->getby_id($id_project);
 $arr_task=$project->get_tasks_in_option($status,$from,$to);
   	 if($arr_task!=0)
				 {
				 foreach($arr_task as $task)
				 	{  
						//if($task->task_status_id!=1)
						  {
							  $task=task::createTaskFromArray($task);
							  $task->get_html_task();//shows all comments and tasks 
						  }
					}
				 }
	else
		{
		 echo "לא נמצאו פניות.... ";	
		}
}//end selector_all_option
if($_POST['do']=='new_message')// checks for new messages
		{
			$user_id=$_POST['user_id'];//for current user
			
			$user=new user();// create user object
			$user->getby_id($user_id);
			if($user->role_type==1 || $user->role_type==6)//checks is the current user an Admin 
				{
				 $arr_project=project::getallproject();					
				}
				else
				{
				$arr_project=$user->get_proj_permissions();//return project_id of user
				}
				
			?>
           
			<?php 
			 $count=0;//new message counter
            foreach($arr_project as $P)//checks all projects according to the users permissions
				{  $temp_project=new project();//create project object
				   $temp_project->getby_id($P); 
				   $arr_task=$temp_project->get_tasks();//Array of tasks per project
				
				 if($arr_task!=0)
				 {
				  foreach($arr_task as $T)// foreach task
				  	{  
					    $temp_task=task::createTaskFromArray($T);
						$bool= if_job($user->getuser_id(),$temp_task->user_id);
						if($user->role_type==4 || $user->role_type==5)
						// if the task is sent from admin show only to restart and not to customers
							{
								$user_meneger = new user();
								$user_meneger->getby_id($temp_task->user_id);
								
								if($user_meneger->role_type==1 || $user_meneger->role_type==6 )
									{
									  $bool=1;	
									 
									}
							}
   						if($temp_task->task_status_id==2 && $bool==0  && $temp_task->task_project_status!=1  )//check if read
							{?> 
							  <div onclick="gotomassage('task',<?php echo $temp_task->get_task_id();?>,null,<?php echo $user_id ?>)">
                              <span id="task_icon" ></span>
							   <p class="p">
                               <span id="new_task" class="color_turkiz">
                                   בפרוייקט 
                                 </span> 
                               <span id="new_task_name"> <?php echo  project::get_name($temp_task->project_id); $count++; ?> </span>
                               <span id="maet" class="color_turkiz"> מאת:</span>
                               <span><?php echo user::get_name($temp_task->user_id); ?> </span>
                               <span  id="customer" class="color_turkiz">לקוח:</span>
                               <span>
                                     <?php
									      $project=new project();
										  $project->getby_id($temp_task->project_id);
										  echo $project->get_customer_name($project->project_customer);
								     ?>
                               </span>
                               <span id="timeago<?php echo $count; ?>" class="timeago  time" title="<?php echo $temp_task->task_date.' '.$temp_task->task_time ;?>">
							       <?php echo $temp_task->task_date.' '.$temp_task->task_time ?>
                               </span>
                               </p>
                               <script> $('#timeago<?php echo $count; ?>').timeago();</script>
                              </div>
							  
							  <?php 
							}
						else 
							{ 
							  $temp_comments=$temp_task->get_comments();//all comments for a task 
							  if($temp_comments!=0)
							  {
							  foreach($temp_comments as $C)
							  	{
									$bool= if_job($user->getuser_id(),$C->user_id);
							    
								 if($C->read==0  && $bool==0  )
								     {  ?>
									  <div  onclick="gotomassage('comments',<?php echo $temp_task->get_task_id();?>,
									        <?php echo $C->getcomment_id();?>,<?php echo $user_id ?>)">
                                            <span id="comment_icon" ></span>
									     <p class="p">
                                             <span id="new_comment" class="color_turkiz">
                                               בפרוייקט 
                                            </span> 
											<span><?php echo  project::get_name($temp_project->getid()); $count++; ?></span>
                                            <span id="from_comment" class="color_turkiz"> מאת:</span> 
                                            <span><?php echo user::get_name($C->user_id); ?> ,</span>
                                            <span id="customer" class="color_turkiz" >לקוח</span>
                                            <span>
											  <?php echo $temp_project->get_customer_name($temp_project->project_customer); ?>
                                            </span>
                                            <span id="timeago_com<?php echo $count ?>" class="timeago time" title="<?php echo $C->comment_date.' '.$C->comment_time;?>">
											   <?php echo $C->comment_date.' '.$C->comment_time; ?>
                                           </span>
                                         </p> 
                                         <script>$('#timeago_com<?php echo $count ?>').timeago(); </script>
                                      </div>
							 <?php }	
									
								 }
							  }
							
							}
					}
				
				 }
				}
				?>
                <script>
                           
							 
						$(document).ready(function(e) {
							$('#hid_sound_new').val(<?php echo $count ?>)
							<?php if($count==0){ ?> 
							 $('#mass #contect').html(""); 
							  $('#mass #bnt').css('background','url(./style/images/massage_off.png) no-repeat bottom right');
							  $('#hid_mass').val('false');
							<?php } else {?>
                           	 $('#mass #bnt').css('background','url(./style/images/message_on.png) no-repeat bottom right');
							 $('#mass  #contect').html(<?php echo $count; ?>+' פניות חדשות מחכות ');//מספר ההודעות שלא נקראו 
                             $('#hid_mass').val('true');
						<?php  }?>
						});
						 function gotomassage(name_class,id_task,id_comment,user_id)
						{
							gomassage(name_class,id_task,id_comment,user_id);	
										 	
						}
                </script>
                
                <?php 
		}//end new_message
		
		if($_POST['do']=='gomassage')
			{
				$user_id=$_POST['user_id'];
				$id_task=$_POST['id_task'];
				$name_class=$_POST['name_class'];
				$id_comment=$_POST['id_comment'];
				$task=new task();
				$task->getby_id($id_task);
				$task->get_html_task();
				$Oj_user=new user();
				$Oj_user->getby_id($user_id);
				if($Oj_user->role_type==1 || $Oj_user->role_type==4 )
				{}
				else
				{
				if($name_class=='comments')
				  {
					 $temp_comments=new comments();
					 $temp_comments->getby_id($id_comment);
					 $temp_comments->read=1;
					 $temp_comments->update();
				  }
				else 
				  {
					  $task->task_status_id=3;
					 
					  $task->update();
				     
				  }
				}
			}// end gomassage
if($_POST['do']=='selector_all_option_customer')
{
 	 $id_customer=$_POST['id_customer'];
	 $from=$_POST['from'];
	 $to=$_POST['to'];	
     $arr_project=project::get_all_project_of_customer($id_customer);
	print_r($arr_project);exit;
	 if($arr_project!=0)
	  {
		 foreach($arr_project as $single_project)
		  { 
			 $arr_task=$single_project->get_tasks_in_option("",$from,$to);
   	       if($arr_task!=0)
				 {
				 ?><span id="name_project" class="color_w"><?php echo  $single_project->project_name ?></span><?php
				 foreach($arr_task as $task)
				 	{   
						  {
							  $task=task::createTaskFromArray($task);
							  $task->get_html_task();//מדפיס פנייה עם כל התגובות 
						  }
					}
				 }
				else
					{
					 //echo "לא נמצאו פניות.... ";	
					}
		 }
	  }
	 else 
	  {
		// echo "אין פרוייקטים לחברה ";  
	  }
	  
	
}//selector_all_option_customer
if($_POST['do']=='get_all_info_customer')
{ 
  $arr_project=project::get_all_project_of_customer($_POST['id_customer']);
 
   dashboard::get_html_bash();
  if($arr_project!=0)
    {
  ?>
    
     <span id="name_customer" class="color_w"><?php echo $arr_project[0]['customer_name'];?></span>
  <?php project::selector_customer($_POST['id_customer']); ?>
  <div id="all_project_customer">
  <?php 
  		$projectIndex=$arr_project[0]['project_id'];
  		$firstRun=false;
	foreach($arr_project as $proj)
	 { ?> 	
	 
		   <?php 
		    if(!$projectIndex==$proj['project_id'])
			 {
			   	 $projectIndex=$proj['project_id'];
				  if($firstRun)echo "</div>";// close of all_task
				 ?>
                 	<span id="name_project" class="color_w"><?php echo  $proj['project_name'] ?></span>
 	                <input id="id_customer" name="id_customer" type="hidden" value="<?php echo $_POST['id_customer'];?>"/>
                    <div id="all_task">
                <?php 
				 $firstRun=true;
			 }//end if(!$projectIndex==$proj['project_id'])
				 
	  
					
							//$task->getby_id($id_task);
							//if($task->task_status_id!=1)
							{
								$task=task::createTaskFromArray($proj);
								$task->get_html_task();
							}
						
	    
     }// end foreach
 }// if($arr_project!=0)
 else // if there are no projects 
  {
	  ?>   <span id="name_customer" class="color_w">אין פרויקטים</span><?
  }
}//get_all_info_customer
if($_POST['do']=='change_status_task')
{    
	  $id_status=$_POST['id_status'];
	  $id_task=$_POST['id_task'];
	  $task = new task;
	  $task->getby_id($id_task);
  if($task->task_status_id!=$id_status)
  {	
  
	  $stringstatus=sprintf("UPDATE `task` SET
						   `task_status_id`=$id_status 
							WHERE `task_id`=$id_task");
				$querystatus=mysql_query($stringstatus)or die(mysql_error().' '. $stringstatus);
	
	$session=$_SESSION['user'];
	
	$user = new user;
	$user->getby_name($session);
	
	$project = new project;
	$project->getby_id($task->project_id);
	$permi=$project->getby_project_id();// all permissions
	$name = user::get_name($task->user_id);
	$name_session= user::get_name($user->getuser_id());
	$name_project = project::get_name($task->project_id);
	$temp_id_task=$task->get_task_id();
	switch($id_status)
	{
	  case 2:
	     $subject = "נפתחה פנייה $temp_id_task על ידי $name לפרוייקט $name_project";
		
	  break;
	  case 4:
	    $subject = " פנייה $temp_id_task נסגרה על ידי $name_session לפרוייקט $name_project";
	  break;	
	  case 5:
	    $subject = " נדחתה פנייה $temp_id_task לפרוייקט $name_project ";;
	  break;
	  default:
	    $subject=" סטאטוס פנייה $temp_id_task השתנה בפרוייקט $name_project";
	  	
	}
	if($permi)
	 { 
	$mail = new PHPMailer();
	foreach($permi as $value)
	 {  
	     $temp_user = new user;
	  	 $temp_user->getby_id($value->user_id);
		  if($temp_user->send_mail==1 && $temp_user->role_type!=2 && $temp_user->role_type!=3)
	 	 {  
		     $mail->AddAddress("$temp_user->user_mail");
			 $haveAddress=true;
		 }
	 }
		    $status1=$task->get_name_status_task($task->task_status_id) ;
		 	$status2=$task->get_name_status_task($id_status);
		    $body=" הפנייה -$task->task_content  \n
			      השתנה ממצב $status1- למצב -$status2 ";
		   
			$mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
			$mail->FromName="RestartIT CRM";
			$mail->From = "restartit@email.com";
			
			$mail->Subject = $subject;
			$mail->Body = "$body";
			$mail->WordWrap = 150;// char count
			$mail->CharSet = 'UTF-8';
		

			if($haveAddress)// if To is included
			 {
					/*if(!$mail->Send())
					{
					 echo "Message was not sent";
					 echo "Mailer Error: " . $mail->ErrorInfo;
					} 
					else { echo "Message was sent";}*/
			 }
	 }// end if($permi)
	  /** send for admin*/
	if($user->role_type!=1)
	{
	  $arr_admin = user::get_user_by_type(1);
	  $mail = new PHPMailer();
	  foreach($arr_admin as $value)
	 {  

		  if($value->send_mail==1)
	 	 { 
		     	$mail->AddAddress("$value->user_mail"); // To address
				$haveAddress=true;
		 }
	 }
		 
		    $status1=$task->get_name_status_task($task->task_status_id) ;
		 	$status2=$task->get_name_status_task($id_status);
		    $body=" הפנייה -$task->task_content  \n
			      השתנה ממצב $status1- 	  למצב -$status2 ";
		   
			$mail->IsSMTP();
			$mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
			$mail->FromName="RestartIT CRM";
			$mail->From = "restartit@email.com";
		
			$mail->Subject =$subject;
			$mail->Body = "$body";
			$mail->WordWrap = 150;// char count
			$mail->CharSet = 'UTF-8';
		
			if($haveAddress)// if there is a Too address
			 {
					/*if(!$mail->Send())
					{
					 echo "Message was not sent";
					 echo "Mailer Error: " . $mail->ErrorInfo;
					} 
					else { echo "Message was sent";}*/
			}
	 	 }
	  
	    $task->task_status_id=$id_status;
	    $task->Close_Date=date('y-m-d');
		$time=getdate(time());
		$task->Close_Time=$time['hours'].':'.$time['minutes'].':'.$time['seconds'];
	    $task->Close_Name=$session;
		$task->update();
  }
 	   
}//change_status_task
if($_POST['do']=='change_status')
{
	 $id_status=$_POST['id_status'];
	  $id_project=$_POST['id_project'];
	  $stringstatus=sprintf("UPDATE `project_status` SET 
				            		 `project_status`=$id_status
									  WHERE  project_id=$id_project ");
				$querystatus=mysql_query($stringstatus)or die(mysql_error().' '. $stringstatus);
}//change_status

if($_POST['do']=='get_tasks_open')//show all tasks in status open or updated and with other status then in order according to user id
{
    $id_user=$_POST['id'];
	$user=new user();
	$user->getby_id($id_user);
	$arr_project=$user->get_proj_permissions();
	
	foreach($arr_project as $idp)
		{  
		     $project=new project();
			 $project->getby_id($idp);
			
			if($project->project_status!=1)
			 {	
				 $arr_task=$project->get_tasks();
				  if($arr_task!=0)
				   { $bool=false;
						
						
					foreach($arr_task as $idt)
						{
							
						   $task=new task();
						   $task->getby_id($idt['task_id']);
							 if(($task->task_status_id==2 || $task->task_status_id==3) &&($task->task_project_status!=1) )
								{ 
									if($bool==false)
										{
											 $project_div="project_".$idp;
												 ?>
														<div id="<?php echo $project_div ?> ">
													   <p class="project_title">
													   <?php echo $project->project_name.'-'.$project->get_customer_name($project->project_customer); ?>
													   </p>
													  
												<?php
												$bool=true;
										}
									$task->get_html_task();	
									
								}
						}
				   }
				  
				?> 
				  
				  </div>
				<?php 
			} //end if $project->project_status!=1
		}
}//get_tasks_open
if($_POST['do']=='insert_message')
{
	  $id_task=$_POST['id_task'];
	  $content=htmlspecialchars($_POST['content'],ENT_COMPAT);
	  
	  $user_id=$_POST['user_id'];
	  $com=new comments();
	  $com->user_id=$user_id;
	  $com->task_id=$id_task;
	  $com->comment_content=$content;
	 
	  $com->insert();
	    
	  $task = new task();
	  $task->getby_id($id_task);
	  $path='./upload/'.$task->project_id.'/';	
	  $txt='./upload/'.$_SESSION['user'].'-'.$_SERVER['REMOTE_ADDR'].'com.txt';
	
	  if(is_file($txt))
	  {
		$filename = $txt;
		$handle = fopen($filename, "rb");
		$contents = fread($handle, filesize($filename));
		fclose($handle);
		$arr=explode('/',$contents);
		
			
		$new_path='./'.$arr[1].'/'.$arr[2].'/'.'C-'.$id_task.'-'.$com->getcomment_id().$arr[3];
	   
	   
	    rename($contents,$new_path);
		$com->attachments=$new_path;
		$com->update();
		unlink($txt);
				
	 }
	 else 
	 {
	  // echo "no file";	 
	  	 
	 }
	  $temp_user = new user;

	  $temp_user->getby_name($_SESSION['user']);//current user
	  /*****************************send mail to Admin *********************************************/
	 if($temp_user->role_type!=1)
	 {
	  $mail = new PHPMailer();
	  $mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
	  $mail->CharSet="utf-8";

	  $mail->From = $mail_crm;
	  $mail->AddAddress($mail_meneger); // To address
	  $task=new task();
	  $task->getby_id($id_task);
	  $number_task=$task->get_task_id();
	 
	  $name_project=project::get_name($task->project_id);
	  $name_user=user::get_name($user_id);
	  $r="<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
	         <tr><td> פרוייקט</td><td>$name_project</td></tr>
			 <tr><td> מספר פנייה</td><td>$number_task</td></tr>
			 <tr><td> מאת</td><td>$name_user</td></tr>
			 <tr><td> תגובה</td><td>$content</td></tr>
		 </table>";
	  $mail->FromName="RestartIT CRM";
	  $mail->Subject = "התקבלה תגובה חדשה  לפניה $number_task על ידי $name_user לפרוייקט $name_project  ";// נושא הדוא"ל
	  $mail->Body = $r; 
	  $mail->IsHTML(true);

	 }
	 /*******************************/
	 $project = new project();
	 $project->getby_id($task->project_id);
	 $permi=$project->getby_project_id();
	
	  if($permi!=0)
	  {
		  $mail = new PHPMailer();
		  foreach($permi as $singlepermi)
		  {
			  $user_temp= new user;
			  $user_temp->getby_id($singlepermi->user_id);
			  
			  if($user_temp->send_mail==1 && $temp_user->role_type!=$user_temp->role_type)
			  {
				  $mail->AddAddress($user_temp->user_mail);
				  $haveAddress=true;
			  }
		  }
				  $mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
				  $mail->CharSet="utf-8";
				  $mail->From = $mail_crm;
				  
				  $task=new task();
				  $task->getby_id($id_task);
				  $number_task=$task->get_task_id();
				 
				  $name_project=project::get_name($task->project_id);
				  $name_user=user::get_name($user_id);
				  $r="<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
						 <tr><td> פרוייקט</td><td>$name_project</td></tr>
						 <tr><td> מספר פנייה</td><td>$number_task</td></tr>
						 <tr><td> מאת</td><td>$name_user</td></tr>
						 <tr><td> תגובה</td><td>$content</td></tr>
					 
					 </table>";
				  $mail->FromName="RestartIT CRM";
				  $mail->Subject = "התקבלה תגובה חדשה  לפניה $number_task על ידי $name_user לפרוייקט $name_project  ";// נושא הדוא"ל
				  $mail->Body = $r; 
				  //$mail->WordWrap = 50;
				  $mail->IsHTML(true);
				 if($haveAddress)
				 {	 
					 /* if(!$mail->Send())
					  {
						// echo "Message was not sent";
						//echo "Mailer Error: " . $mail->ErrorInfo;
						
					  }
					  else
					  {
						//echo "Message has been sent";
						
					  } */
				 }
				
	  }
	  /****************************************************************************************************************/
	$com->getby_id($com->getcomment_id());
	return $com->get_html_com();
}//insert_message
if($_POST['do']=='send_new_task')
{
			$id_project=$_POST['id_project'];
			$hid_id_user=$_POST['hid_id_user'];
			$content=$_POST['content_textarea'];
			$content=htmlspecialchars($content,ENT_COMPAT);
			$inp_link=$_POST['inp_link'];
			$Browser=$_POST['Browser'];
			$path='./upload/'.$id_project.'/';	
			$txt='./upload/'.$_SESSION['user'].'-'.$_SERVER['REMOTE_ADDR'].'.txt';
	 $new_task=new task();
	         $new_task->project_id=$id_project;
			 $new_task->user_id=$hid_id_user;
			 $new_task->task_content=$content;
			 $new_task->task_link=$inp_link;
			 $new_task->Browser=$Browser;
			 $new_task->task_attachments='';	
			 $idProjectStatus = new project();
			 $idProjectStatus->getby_id($id_project);
            $new_task->task_project_status=$idProjectStatus->project_status;
			 $temp_user=new user();
			 $temp_user->getby_id($hid_id_user);
			 if($temp_user->role_type==1){$new_task->task_status_id=2;}
			
	$new_task->insert();
	
			
	
	 if(is_file($txt))
	 {
		$filename = $txt;
		$handle = fopen($filename, "rb");
		$contents = fread($handle, filesize($filename));
		fclose($handle);
		
		$arr=explode('/',$contents);
		$new_path='./'.$arr[1].'/'.$arr[2].'/'.'T-'.$new_task->get_task_id().$arr[3];
	    rename($contents,$new_path);
		$new_task->task_attachments=$new_path;
		$new_task->update();
		unlink($txt);
				
	 }
	 
	 /****************************************end for send mail to Admin*******************************************************/
 if($temp_user->role_type!=1)
	{    
	  $mail = new PHPMailer();
	  $mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
	  $mail->CharSet="utf-8";
	  $mail->From = $mail_crm;
	  $mail->AddAddress($mail_meneger);
	  $name_project=project::get_name($id_project);
	  $name_user=user::get_name($hid_id_user);
	  $number_task=$new_task->get_task_id();
	  $mail->FromName="RestartIT CRM";
	  $r="<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
	         <tr><td> פרוייקט</td><td>$name_project</td></tr>
			 <tr><td> פנייה</td><td>$number_task</td></tr>
			 <tr><td> מאת</td><td>$name_user</td></tr>
			 <tr><td> פנייה</td><td>$content</td></tr>
		 </table>";
	  
	  $mail->Subject = "נפתחה פנייה $number_task על ידי $name_user לפרוייקט $name_project ";
	  $mail->Body = $r; 
	  $mail->IsHTML(true);
	 /* if(!$mail->Send())
	  {
		// echo "Message was not sent";
		// echo "Mailer Error: " . $mail->ErrorInfo;
	  }
	  else
	  {
		//echo "Message has been sent";
	  }*/
	  /****send mail to all conected to project****/
	  
	}
	 
	 $project = new project();
	 $project->getby_id($new_task->project_id);
	 
	 $permi=$project->getby_project_id();
     
	  if($permi!=0)
	  {
		  $mail = new PHPMailer();
		  foreach($permi as $singlepermi)
		  {
			  $user_temp= new user;
			  $user_temp->getby_id($singlepermi->user_id);
			  
			  if($user_temp->send_mail==1 && $temp_user->role_type!=$user_temp->role_type)
			  {
				   $mail->AddAddress($user_temp->user_mail); // To address
				   $haveAddress=true;
			  }
		  }
				  $mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
				  $mail->CharSet="utf-8";
				  $mail->From = $mail_crm;
				 
				  $task=new task();
				  $task->getby_id($new_task->get_task_id());
				  $number_task=$task->get_task_id();
				 
				  $name_project=project::get_name($task->project_id);
				  $name_user=user::get_name($hid_id_user);
				  $r="<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
						 <tr><td> פרוייקט</td><td>$name_project</td></tr>
						 <tr><td> פנייה</td><td>$number_task</td></tr>
						 <tr><td> מאת</td><td>$name_user</td></tr>
						 <tr><td> פנייה</td><td>$content</td></tr>
					 </table>";
	  
	              $mail->Subject = "נפתחה פנייה $number_task על ידי $name_user לפרוייקט $name_project ";
				  $mail->Body = $r; 
				   $mail->FromName="RestartIT CRM";
				  $mail->IsHTML(true);
				 if($haveAddress)
				  {	
					  /*if(!$mail->Send())
					  {
						// echo "Message was not sent";
						//echo "Mailer Error: " . $mail->ErrorInfo;
						
					  }
					  else
					  {
						//echo "Message has been sent";
						
					  } */
				  }
			
	  }
	 /**************************************************************************************************/
	  /****************************************send mail to customer contact**************************************************/
	  /*$project=new project();
	  $project->getby_id($id_project);
	  $user_contect=$project->get_contect_customer();
	
	 if(!$user_contect==0)
	  {
		foreach($user_contect as $SingleUserContact)
		 {
	   
		  $mail_contect = new PHPMailer();
		  $mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
		  $mail_contect->CharSet="utf-8";
		 // $mail_contect->Host = "smtp.email.com";
		  $mail_contect->From = $mail_crm;
		  $mail_contect->AddAddress($SingleUserContact->user_mail);
		  $name_project=project::get_name($id_project);
		  $name_user=user::get_name($hid_id_user);
		  $number_task=$new_task->get_task_id();
		  $mail_contect->FromName="RestartIT CRM";
		  $r="<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
				 <tr><td> פרוייקט</td><td>$name_project</td></tr>
				 <tr><td> פנייה</td><td>$number_task</td></tr>
				 <tr><td> מאת</td><td>$name_user</td></tr>
				 <tr><td> פנייה</td><td>$content</td></tr>
			 </table>";
		  
		  $mail_contect->Subject = "נפתחה פנייה $number_task על ידי $name_user לפרוייקט $name_project ";
		  $mail_contect->Body = $r; 
		  //$mail_contect->WordWrap = 50;
		  $mail_contect->IsHTML(true);
		  if(!$mail_contect->Send())
		  {
			// echo "Message was not sent";
			// echo "Mailer Error: " . $mail->ErrorInfo;
		  }
		  else
		  {
			//echo "Message has been sent";
		  }
		 }
	  }*/
	 $new_task->getby_id($new_task->get_task_id());
	 return  $new_task->get_html_task();
}//send_new_task

?>